﻿using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCCommon.OAEnums;
using BCDto.MySql.Admin;
using BlackCoffeeRESTful.Configs;
using BCDto.OA.Attendance.AttendanceGroup;
using BCService.OA.Attendance.AttendanceGroup;

namespace BlackCoffeeRESTful.Controllers.Management.OA.Attendance
{
    /// <summary>
    /// 考勤组信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/OA/[controller]")]
    public class AttendanceGroupController : ManagementBaseController
    {
        private readonly IAttendanceGroupService attendanceGroupService;

        public AttendanceGroupController(IAttendanceGroupService attendanceGroupService)
        {
            this.attendanceGroupService = attendanceGroupService;
        }

        /// <summary>
        /// 获取一条考勤组信息
        /// </summary>
        /// <param name="groupId">指定的考勤组Id</param>
        /// <returns></returns>
        [HttpGet("{group_id}")]
        [ProducesResponseType(typeof(AttendanceGroupViewDto), 200)]
        public IActionResult Get([FromRoute(Name = "group_id")] long groupId)
        {
            return Ok(this.attendanceGroupService.GetByGroupId(groupId));
        }

        /// <summary>
        /// 分页查询考勤组信息列表
        /// </summary>
        /// <param name="groupName">考勤名称</param>
        /// <param name="type">考勤类型</param>
        /// <param name="checkInMode">打卡方式</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <param name="ascending">排序</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<AttendanceGroupDto>), 200)]
        public IActionResult Query(
             [FromQuery(Name = "group_name")]string groupName,
             [FromQuery(Name = "type")]AttendanceType? type,
             [FromQuery(Name = "check_in_mode")]CheckInMode? checkInMode,
             [FromQuery(Name = "ascending")]bool ascending = true,
            int pageIndex = 1,
            int pageSize = 10)
        {
            var data = attendanceGroupService.GetList(this.Admin.IsMain ? new AdminDto { AdminId = 0 } : this.Admin, ascending, pageIndex, pageSize, groupName, type, checkInMode);
            return Ok(data);
        }

        #region 新增操作


        /// <summary>
        /// 创建固定班次考勤组
        /// </summary>
        /// <param name="attendanceFixed">固定时间信息</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("创建固定班次考勤组")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 201)]
        public IActionResult CreateFixed([FromBody]AttendanceFixedGroupRequestDto attendanceFixed)
        {
            var result = this.attendanceGroupService.CreateFixedGroup(this.Admin, attendanceFixed);
            return Created(string.Format("api/v1/sim/OA/AttendanceGroup/{0}", result.GroupId), result);
        }


        /// <summary>
        /// 创建排班制考勤组
        /// </summary>
        /// <param name="attendanceTurn">排班时间信息</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("创建排班制考勤组")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 201)]
        public IActionResult CreateTurn([FromBody]AttendanceTurnGroupRequestDto attendanceTurn)
        {
            var result = this.attendanceGroupService.CreateTurnGroup(this.Admin, attendanceTurn);
            return Created(string.Format("api/v1/sim/OA/AttendanceGroup/{0}", result.GroupId), result);
        }

        /// <summary>
        /// 创建自由时间考勤组
        /// </summary>
        /// <param name="groupNoneRequestDto">自由时间信息</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("创建自由时间考勤组")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 201)]
        public IActionResult CreateNone([FromBody]AttendanceNoneGroupRequestDto groupNoneRequestDto)
        {
            var result = this.attendanceGroupService.CreateNoneGroup(this.Admin, groupNoneRequestDto);
            return Created(string.Format("api/v1/sim/OA/AttendanceGroup/{0}", result.GroupId), result);
        }

        #endregion

        #region 修改

        /// <summary>
        /// 修改固定考勤组信息
        /// </summary>
        /// <param name="groupId">指定的考勤组Id</param>
        /// <param name="attendanceFixed">固定时间信息</param>
        /// <returns></returns>
        /// <response code="200">修改后的考勤组信息。</response>
        [HttpPut("[action]/{group_id}")]
        [Log("修改固定考勤组信息")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 200)]
        public IActionResult FixedGroup([FromRoute(Name = "group_id")] long groupId, [FromBody]AttendanceFixedGroupRequestDto attendanceFixed)
        {
            return this.Ok(this.attendanceGroupService.UpdateFixedGroup(this.Admin, groupId, attendanceFixed));
        }

        /// <summary>
        /// 修改排班考勤组信息
        /// </summary>
        /// <param name="groupId">指定的考勤组Id</param>
        /// <param name="turnGroupRequestDto">排班时间信息</param>
        /// <returns></returns>
        /// <response code="200">修改后的考勤组信息。</response>
        [HttpPut("[action]/{group_id}")]
        [Log("修改排班考勤组信息")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 200)]
        public IActionResult TurnGroup([FromRoute(Name = "group_id")] long groupId, [FromBody]AttendanceTurnGroupRequestDto turnGroupRequestDto)
        {
            return this.Ok(this.attendanceGroupService.UpdateTurnGroup(this.Admin, groupId, turnGroupRequestDto));
        }

        /// <summary>
        /// 修改自由时间考勤组信息
        /// </summary>
        /// <param name="groupId">指定的考勤组Id</param>
        /// <param name="noneGroupRequestDto">自由时间信息</param>
        /// <returns></returns>
        /// <response code="200">修改后的考勤组信息。</response>
        [HttpPut("[action]/{group_id}")]
        [Log("修改自由时间考勤组信息")]
        [ProducesResponseType(typeof(AttendanceGroupDto), 200)]
        public IActionResult NoneGroup([FromRoute(Name = "group_id")] long groupId, [FromBody]AttendanceNoneGroupRequestDto noneGroupRequestDto)
        {
            return this.Ok(this.attendanceGroupService.UpdateNoneGroup(this.Admin, groupId, noneGroupRequestDto));
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除一条考勤组信息
        /// </summary>
        /// <param name="groupId">指定的考勤组Id</param>
        /// <returns></returns>
        [HttpDelete("{group_id}")]
        [Log("删除一条考勤组信息")]
        public IActionResult Delete([FromRoute(Name = "group_id")] long groupId)
        {
            return Ok(this.attendanceGroupService.Delete(this.Admin, groupId));
        }

        #endregion
    }
}